package csu.web.mypetstore.filter;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Logger;
import csu.web.mypetstore.persistence.impl.LoggerDaoImpl;

import javax.annotation.processing.Filer;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LoggerFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

        String uri=httpServletRequest.getRequestURI().substring(1);
        //String addItemToCart = null;
        int index = uri.indexOf('/');
        uri = uri.substring(index+1);

        System.out.println("当前URI为"+uri);

        String action = null;

        // 仅当用户登录后才记录日志
        HttpSession session = httpServletRequest.getSession();
        Account loginAccount = (Account)session.getAttribute("loginAccount");
        if(loginAccount!=null){
            System.out.println(loginAccount.getUsername());
        }

        // 时间戳转换为日期字符串
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        System.out.println(dateString);



        if(loginAccount!=null){
            if(uri.equals("categoryForm")){
                // categoryForm?categoryId=FISH
                String categoryId = httpServletRequest.getParameter("categoryId");

                action = "浏览"+categoryId+"种类";
            }else if(uri.equals("productForm")){
                // productForm?productId=FI-FW-01
                String productId = httpServletRequest.getParameter("productId");
                action = "浏览"+productId+"商品";
            } else if (uri.equals("itemForm")) {
                // itemForm?itemId=EST-16
                String itemId = httpServletRequest.getParameter("itemId");
                action = "浏览"+itemId+"品种";
            } else if(uri.equals("addItemToCart")){
                // addItemToCart?workingItemId=EST-4
                String addItemToCart = httpServletRequest.getParameter("workingItemId");
                action = "添加"+addItemToCart+"进入购物车";
            }else if(uri.equals("newOrderForm")){
                // newOrderForm
                String newOrderForm = httpServletRequest.getParameter("newOrderForm");
                action = "生成订单";
            }
            if(action!=null){
                final LoggerDaoImpl loggerDao = new LoggerDaoImpl();
                loggerDao.setLogger(new Logger(loginAccount.getUsername(),action,dateString));
            }
        }
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {
    }

//    public void
}
